export { fileRead, normalizeFileName, MIME_CODE_TABLE } from './file'
export { arrayRemove } from './array'

export const worksIdStorageKey = 'local:works'

export function isNil(target: unknown): target is null | undefined {
  return target === null || target === undefined
}

export function isObject<T extends object>(target: unknown): target is T {
  return Object.prototype.toString.call(target) === '[object Object]'
}

export function uuid() {
  return [Date.now(), Math.random() * 1000000, Math.random() * 1000000]
    .map((num) => Math.trunc(num).toString(36))
    .join('-')
}

export function toMap<T extends {}, PK extends keyof T = keyof T>(
  target: T[],
  pk: PK
): Record<T[PK] extends PropertyKey ? T[PK] : never, T>
export function toMap<
  T extends Record<string | number, unknown>,
  PK extends keyof T = keyof T,
  VK extends keyof T = keyof T
>(target: T[], pk: PK, vk: VK): Record<PK extends PropertyKey ? PK : never, T[VK]>
export function toMap<
  T extends Record<string | number, unknown>,
  PK extends keyof T = keyof T,
  VK extends keyof T = keyof T
>(target: T[], pk: PK, vk?: VK): any {
  const map: Record<any, any> = {}

  target.forEach((item) => {
    map[item[pk] as string] = vk ? item[vk] : item
  })

  return map
}
